package com.workManager.config;

import com.github.pagehelper.PageInfo;
import com.workManager.base.LoginHelper;
import com.workManager.pojo.TdUser;
import com.workManager.service.RoleService;
import com.workManager.service.UserService;
import com.workManager.util.WebUtil;
import com.workManager.vo.CurrentUserVO;
import com.workManager.vo.RoleFunctionVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Thinkpad on 2017/1/12 0012.
 */
@Component
public class Realm extends AuthorizingRealm {


    private final Logger logger = LogManager.getLogger(Realm.class);

    @Autowired
   private UserService userService;

    @Autowired
    private RoleService roleService;



    //权限
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        CurrentUserVO user = WebUtil.getCurrentUserVO();

        if (user != null) {
            List<String> funCodeList = roleService.getRoleFunctionCodeByUserId(user.getUid());
            if (funCodeList != null && funCodeList.size() > 0) {
                for (String code : funCodeList) {
                    info.addStringPermission(code);
                }
            }
        }

        return info;
    }

    //登陆验证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        TdUser tdUser = userService.queryUserByLoginCode(token.getUsername());
        //System.out.println("reaml中过滤出来的用户------------："+tdUser.getUserName());
        if(tdUser!=null){

            StringBuilder sb = new StringBuilder(100);
            for(int i=0;i<token.getPassword().length;i++){
                sb.append(token.getPassword()[i]);
            }
            if(tdUser.getPassword().equals(sb.toString())){
                CurrentUserVO vo = LoginHelper.initCurrentUserVO(tdUser);

                WebUtil.saveCurrentUser(tdUser.getUserId());
                WebUtil.saveCurrentUser(vo);
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("loginCode",tdUser.getUserName());
                userService.updateLoginTimeByLogCode(map);

                AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(tdUser.getLoginCode(),tdUser.getPassword(),tdUser.getUserName());
                return  authenticationInfo;
            }
            logger.warn("USER ["+token.getUsername()+"] PASSWORD IS WRONG");
            return  null;

        }else {
            logger.warn("no user:"+token.getUsername());
            return  null;
        }

    }
}
